﻿using Shop.Model.DB;
using WeDonekRpc.SqlSugar;

namespace Shop.Repository.lmpl
{
    internal class CouponCodeRepository : ShopBaseRepository<DBShopCouponCode, long>, ICouponCodeRepository
    {
        public CouponCodeRepository (ISqlClientFactory factory) : base(factory)
        {
        }

        public void Adds (List<DBShopCouponCode> adds)
        {
            if (adds.Count >= 1000)
            {
                base.InsertTable(adds);
            }
            else
            {
                base.Insert(adds);
            }
        }

        public void Sync (long couponId, List<DBShopCouponCode> adds)
        {
            if (adds.Count >= 1000)
            {
                using (ILocalTransaction tran = base.ApplyTran())
                {
                    base.Delete(a => a.CouponId == couponId);
                    base.InsertTable(adds);
                    tran.Commit();
                }
            }
            else
            {
                ISqlQueue<DBShopCouponCode> queue = base.BeginQueue();
                queue.Delete(a => a.CouponId == couponId);
                queue.Insert(adds);
                queue.Submit();
            }
        }
    }
}
